//
//  YDUnBindCardViewController.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/1/4.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDUnBindCardViewController.h"
#import "YDBankListCell.h"
#import "YDUnBindBankCell.h"

@interface YDUnBindCardViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation YDUnBindCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableView];
    self.title = @"我的银行卡";
    if (!self.isHideUnBindBtn) {
        [self setNav];
    }
    
}


- (void)setNav
{
    UIBarButtonItem *rightItemBtn = [[UIBarButtonItem alloc] initWithTitle:@"解绑" style:UIBarButtonItemStylePlain  target:self action:@selector(unbind)];
  
    self.navigationItem.rightBarButtonItem = rightItemBtn;
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
    
}

- (void)unbind
{

    
    PZTipsAlertView *alert = [PZTipsViewManger shareDoubleTipsAlertViewTitle:@"提示" Mesage:@"确定解绑次张银行卡吗？" CancleTitle:@"取消" SureTitle:@"确定" CancleBlock:^{
        [self.pz_popupController dismiss];
    } SureBlock:^{
        [self unbindCartRequest];
        [self.pz_popupController dismiss];
    }];
    
    self.pz_popupController = [[PZPopUpController alloc] init];
    [self.pz_popupController dropAnimatedWithRotateAngle:30];
    [self.pz_popupController presentContentView:alert duration:0.75 springAnimated:YES];
}


-(void)unbindCartRequest
{
    //POST /bindCard/unbundling/{id}
    [[SYProgressHUD sharedInstance] showMBHudWithSting:kLoadingText];
    NSString *url = [NSString stringWithFormat:@"%@%@",@"/bindCard/unbundling/" ,@(self.cardModel.Id)];
    NSString *baseUrl = [NSString stringWithFormat:@"%@%@",baseUrlHttp , url];
    NSString *authToken = [[SYUserDataManger sharedInstance] getUserToken];
    BaseNetRequest *request = [[BaseNetRequest alloc] initWithRequestMethod:POSTMethod];
    [request sessionRequestWithDictionary:nil andURLString:baseUrl andAuthToken:authToken WithSuccessBlock:^(NSDictionary *resultDict) {
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        DDLog(@"resultDict = %@" , resultDict)
        if ([resultDict[@"code"] integerValue] == 200) {
            
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                            WithType:NavTipsViewTypeSuccess
                                                                text:@"解绑银行卡成功"
                                                          isExistNav:YES];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else{
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                            WithType:NavTipsViewTypeSuccess text:resultDict[@"message"]
                                                          isExistNav:YES];
        }
    } failBlock:^(NSError *error) {
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                        WithType:NavTipsViewTypeSuccess text:@"解绑银行卡失败"
                                                      isExistNav:YES];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else
    {
        return 3;
    }
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        YDBankListCell *cell = [YDBankListCell cellWithTableView:tableView];
        cell.cardModel = self.cardModel;
        return cell;
    }else
    {
        YDUnBindBankCell *cell = [YDUnBindBankCell cellWithTableView:tableView];
        [cell cellWithModel:self.cardModel index:indexPath.row];
        return cell;
    }
  
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
       return  isPhoneX ? rateX812(132) :rate750(132);
    }else
    {
        return  isPhoneX ? rateX812(40) :rate750(40);
    }
}

/*******tableView********/
-(UITableView *)tableView
{
    if (_tableView == nil) {
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LKScreenW, self.view.height - self.tabBarController.tabBar.height) style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.backgroundColor = NumberColor(255, 255, 255);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.rowHeight = isPhoneX ? rateX812(100) :rate750(100);
        tableView.tableFooterView = [UIView new];
        if (isIos11) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView = tableView;
    }
    return _tableView;
}

@end
